import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class GamePlay {
	private static BufferedReader data = new BufferedReader(new InputStreamReader(System.in));
	private static String line;
	
	
	public static void main(String[] args){
		int startnum = getStartNum();
		
		if (startnum <= 0){
			startnum = getStartNum();
			System.out.println(startnum);
		}else if (startnum == -1){
			System.exit(0);
		}
		
		while(startnum != 1){
			startnum = player1(startnum);
			if (startnum == 0){
				startnum = player1(startnum);
				System.out.println(startnum);
			}else if (startnum == -1){
				System.exit(0);
			}else if(startnum == 1){
				System.out.println("Player 2 Loses!");
			}
			
			startnum = player2(startnum);
			if (startnum == 0){
				startnum = player2(startnum);
				System.out.println(startnum);
			}else if (startnum == -1){
				System.exit(0);
			}else if (startnum == 1){
				System.out.println("Player 1 Loses!");
			}
		}
		System.exit(0);
	}
	
	private static int getStartNum(){
		System.out.println("PLAYER 2 Please Pick A Starting Value(type quit to quit):  ");
		try {
			line = data.readLine(); // contains user input.
			if (line == null || line.equals("")) { // checks to see for no input.
				System.out.println("Nothing was entered.  Please Pick A Starting Value.");
				return 0;
			}
			if (line.equals("quit")) { // checks for user desire to exit program.
				System.out.println("Exiting the program.");
				return -1;
			}
		} catch (IOException e) {
			System.err.println("Come back later.  I have to take a dump.");
		} // error message.
		
		return Integer.parseInt(line);
	}
	
	private static int player1(int num){
		System.out.println("PLAYER 1 Please Pick A factor of " + num + "(type quit to quit):  ");
		try {
			line = data.readLine(); // contains user input.
			if (line == null || line.equals("")) { // checks to see for no input.
				System.out.println("Nothing was entered.  Please Entera Factor of " + num + ".");
				return 0;
			}
			if (line.equals("quit")) { // checks for user desire to exit program.
				System.out.println("Exiting the program.");
				return -1;
			}
		} catch (IOException e) {
			System.err.println("Come back later.  I have to take a dump.");
		} // error message.
		return num - Integer.parseInt(line);
	}
	
	private static int player2(int num){
		System.out.println("PLAYER 2 Please Pick A factor of " + num + "(type quit to quit):  ");
		try {
			line = data.readLine(); // contains user input.
			if (line == null || line.equals("")) { // checks to see for no input.
				System.out.println("Nothing was entered.  Please Entera Factor of " + num + ".");
				return 0;
			}
			if (line.equals("quit")) { // checks for user desire to exit program.
				System.out.println("Exiting the program.");
				return -1;
			}
		} catch (IOException e) {
			System.err.println("Come back later.  I have to take a dump.");
		} // error message.
		return num - Integer.parseInt(line);
	}
}
